'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 引导
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var mongoose = require('mongoose');
var dataApi = require('../libs/dataApi');
var _ = require('lodash');

/**
 * 引导
 */
var guideSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 新手指导ID存放列表
    guide_list: [],

    // 已领取奖励列表 
    state_reward_list: {
        type: mongoose.Schema.Types.Mixed,
        default: {}
    },

    // 开始的进度
    start_progress: {
        type: Number,
        default: 0
    },

    // 结束的进度
    progress: {
        type: Number,
        default: 0
    }
});

guideSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 增加已购买type和状态
 * @return {[type]} [description]
 */
guideSchema.methods.addGuideId = function (guide_id) {
    this.state_reward_list[guide_id] = this.state_reward_list[guide_id] || 0;
    this.state_reward_list[guide_id] = 1;
    this.markModified('state_reward_list');
};

/**
 * 获取引导奖励领取状态
 * @return {[type]} [description]
 */
guideSchema.methods.getGuideId = function (guide_id) {
    return this.state_reward_list[guide_id] || 0;
};

guideSchema.methods.getGuideList = function () {
    return this.guide_list;
};

/**
 * 增加进度列表值
 * @return {[type]} [description]
 */
guideSchema.methods.addGuideList = function (guide_type) {
    for (var i = 0; i < this.getGuideList().length + 1; i++) {
        _.indexOf(this.guide_list, guide_type);
        if (_.indexOf(this.guide_list, guide_type) == -1) {
            this.guide_list.push(guide_type);
            return true;
        } else {
            return false;
        }
    }
};

guideSchema.methods.getStartProgress = function () {
    return this.start_progress;
};

guideSchema.methods.setStartProgress = function (progress) {
    progress = +progress;
    if (this.start_progress < progress) {
        this.start_progress = +progress;
    }
};

guideSchema.methods.setProgress = function (progress) {
    progress = +progress;
    if (!this.progress || this.progress < progress) {
        this.progress = +progress;
    }
};

/**
 * 获取进度
 * @return {[type]} [description]
 */
guideSchema.methods.getProgress = function () {
    return this.progress;
};

guideSchema.methods.push = function () {

};

guideSchema.methods.pushModify = function () {

};

if (!guideSchema.options.toJSON) {
    guideSchema.options.toJSON = {};
}
/* jshint unused:false */
guideSchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Guide', guideSchema);
